package cn.lena.weblog.controller;

import cn.lena.weblog.domain.Passage;
import cn.lena.weblog.domain.User;
import cn.lena.weblog.service.CatagoryService;
import cn.lena.weblog.service.KeywordService;
import cn.lena.weblog.service.PassageService;
import cn.lena.weblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/passage")
public class PassageController {

	@Autowired
	private PassageService passageService;

	@Autowired
	private CatagoryService catagoryService;

	@Autowired
	private KeywordService keywordService;

	@Autowired
	private UserService userService;

	@GetMapping("renew/{id}")
	public String renewPassage(@PathVariable Integer id,HttpSession session, Model model) {
		int i=passageService.renewPassage(id);
		if(i==1){
			model.addAttribute("Msg","删除成功");
			// 更新文章列表
			model.addAttribute("passageList",
					passageService.findDelete(((User)session.getAttribute("user")).getUserId()));
		}else{
			model.addAttribute("Msg","删除失败");
		}
		return "user/blog-delete";
	}

	@GetMapping("del/{id}")
	public String delPassage(@PathVariable Integer id,HttpSession session, Model model) {
		Integer catagory=(Integer)session.getAttribute("catagoryId");
		int i=passageService.deletePassage(id);
		if(i==1){
			model.addAttribute("Msg","删除成功");
			// 更新文章列表
			List<Passage> passages = passageService.findByCatagoryAndUser(((User)session.getAttribute("user")).getUserId(), catagory);
			model.addAttribute("catagoryName",passages.get(0).getCatagoryName());
			model.addAttribute("passageList",passages);
		}else{
			model.addAttribute("Msg","删除失败");
		}
		return "user/catagory-passage";
	}

	@GetMapping("myblog/del/{id}")
	public String delMyBlog(@PathVariable Integer id,HttpSession session, Model model) {
		int i=passageService.deletePassage(id);
		if(i==1){
			model.addAttribute("Msg","删除成功");
			// 更新文章列表
			model.addAttribute("passageList",
					passageService.findByUserId(((User)session.getAttribute("user")).getUserId()));
		}else{
			model.addAttribute("Msg","删除失败");
		}
		return "user/blog-my";
	}

	@GetMapping("mydraft/del/{id}")
	public String delMyDraft(@PathVariable Integer id,HttpSession session, Model model) {
		int i=passageService.deletePassage(id);
		if(i==1){
			model.addAttribute("Msg","删除成功");
			// 更新文章列表
			model.addAttribute("passageList",
					passageService.findDraft(((User)session.getAttribute("user")).getUserId()));
		}else{
			model.addAttribute("Msg","删除失败");
		}
		return "user/blog-draft";
	}

	@RequestMapping("/findPassage/{id}")	//t 前台页面查询某一篇文章
	public String findPassage(@PathVariable Integer id,HttpSession session, Model model){
		if (session.getAttribute("user") == null) {
			model.addAttribute("login",false);
		}else{
			model.addAttribute("login",true);
		}
		Passage passage = passageService.findById(id);

		model.addAttribute("passage",passage);
		session.setAttribute("currentUser",passage.getUserId());
		User user = userService.findUserMore(passage.getUserId());
		user.setRegistAge((int)Duration.between(LocalDateTime.now(),user.getJoinDate()).toDays()/365);
		model.addAttribute("author",user);
		return "passage";
	}

	@GetMapping("/findPassages")		//t
	public String findByCreateDate(Model model,HttpSession session){
		if (session.getAttribute("user") == null) {
			model.addAttribute("login",false);
		}else{
			model.addAttribute("login",true);
		}
		model.addAttribute("latelyPassages",passageService.findByCreateDate());
		model.addAttribute("keywords",keywordService.findTop10());
		return "index";		//首页显示博文信息
	}

	@RequestMapping("/findById/{id}")	//t 后台管理页面查询某一篇特定文章
	public String findByPassageId(@PathVariable Integer id,HttpSession session, Model model){
		if (session.getAttribute("user") == null) {
			return "user/login";    //用户没有登录，或者登录已经失效
		}
		model.addAttribute("passage",
				passageService.findById(id));
		return "user/passage";
	}

	@GetMapping("/findByCatagory/{id}")	//t
	public String findByCatagory(@PathVariable Integer id, HttpSession session, Model model) {
		if (session.getAttribute("user") == null) {
			return "user/login";    //用户没有登录，或者登录已经失效
		}
		session.setAttribute("catagoryId",id);
		List<Passage> passages = passageService.findByCatagoryAndUser(((User)session.getAttribute("user")).getUserId(), id);
		if(passages.size()==0){
			model.addAttribute("Msg","该分类下没有文章");
			return "user/catagory";
		}
		model.addAttribute("catagoryName",passages.get(0).getCatagoryName());
		model.addAttribute("passageList",passages);
		return "user/catagory-passage";
	}

	@GetMapping("/findByCollectCatagory/{id}")	//t
	public String findByCollectCatagory(@PathVariable Integer id, HttpSession session, Model model) {
		System.out.println(id);
		if (session.getAttribute("user") == null) {
			return "user/login";    //用户没有登录，或者登录已经失效
		}
		List<Passage> passages = passageService.findByCatagoryAndUser(((User)session.getAttribute("user")).getUserId(), id);
		if(passages.size()==0){
			model.addAttribute("Msg","该分类下没有文章");
			return "user/catagory";
		}
		model.addAttribute("catagoryName",passages.get(0).getCatagoryName());
		model.addAttribute("passageList",passages);
		return "user/collect-passage";
	}

	@RequestMapping("/findCollectPassage")	//t
	public String findCollectPassage(HttpSession session,Model model) {
		if (session.getAttribute("user") == null) {
			return "user/login";    //用户没有登录，或者登录已经失效
		}
		model.addAttribute("passageList",
				passageService.findCollectPassage(((User)session.getAttribute("user")).getUserId()));
		return "user/collect-passage";
	}

	@RequestMapping("/write")		//t
	public String writeBlog(HttpSession session,Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		model.addAttribute("catagoryList",
				catagoryService.findByUserId(((User)session.getAttribute("user")).getUserId()));
		return "user/blog-write";
	}

	@RequestMapping("/add")	//t
	public String add(Passage passage,HttpSession session, Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		passage.setUserId(((User)session.getAttribute("user")).getUserId());
		passage.setCreateDate(LocalDateTime.now());
		int i = passageService.addPassage(passage);
		if(i==1){
			model.addAttribute("msg","添加成功");
			return "user/blog-write";
			//return "user/blog-my";
		}else{
			model.addAttribute("msg","添加失败");
			return "user/blog-write";
		}

	}

	@RequestMapping("/findDraft")//t
	public String findDraft(HttpSession session,Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		model.addAttribute("passageList",
				passageService.findDraft(((User)session.getAttribute("user")).getUserId()));
		return "user/blog-draft";
	}

	@RequestMapping("/findDelete")//t
	public String findDelete(HttpSession session,Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		model.addAttribute("passageList",
				passageService.findDelete(((User)session.getAttribute("user")).getUserId()));
		return "user/blog-delete";
	}

	@RequestMapping("/findByUserId")//t
	public String findByUserId(HttpSession session,Model model){
		if(session.getAttribute("user")==null){
			return "user/login";	//用户没有登录，或者登录已经失效
		}
		model.addAttribute("passageList",
				passageService.findByUserId(((User)session.getAttribute("user")).getUserId()));
		return "user/blog-my";
	}


}
